<?php
/**
 *翻译管理
*/

namespace app\admin\controller\Record;
use PhpOffice\PhpWord\TemplateProcessor;
use app\admin\controller\Admin;
use think\facade\App;
use think\facade\Db;
use xhadmin\service\admin\record\TranslatorService;
use xhadmin\db\Translator as TranslatorDb;
use think\facade\Cache;

class Translator extends Admin {


	/*翻译管理*/
	function index(){
        //p(session('admin'));exit;
        if (!$this->request->isAjax()){
			return $this->display('record/translator/index');
		}else{
			$limit  = $this->request->post('limit', 0, 'intval');
			$offset = $this->request->post('offset', 0, 'intval');
			$page   = floor($offset / $limit) +1 ;

			$name = $this->request->param('uname', '', 'strip_tags,trim');
            if(!empty($name)){
                $where['name'] = ['like','%'.$name.'%'];
            }
//            if(!empty($mobile)){
//                $where['mobile'] = ['like','%'.$mobile.'%'];
//            }
			$limit = ($page-1) * $limit.','.$limit;
            //p($where);exit;
			try{
				$res = TranslatorService::pageList(formatWhere($where),$limit,'*',$orderby = '');
				$list = $res['list'];
			}catch(\Exception $e){
				exit($e->getMessage());
			}

			$data['rows']  = $list;
			$data['total'] = $res['count'];
			return json($data);
		}
	}

	/*添加*/
	function add(){
		if (!$this->request->isPost()){
			return $this->display('record/translator/info');
		}else{
            //$data = $this->request->post();
            //p($data);exit;
            $postField = 'id,name,birth_day,sex,address,passport_idcard_no';
			$data = $this->request->only(explode(',',$postField),'post');
			//p($data);exit;

            $is_idcard = TranslatorDb::countList(['name'=>$data['name']]);
            if($is_idcard) {
                return json(['status'=>'01','msg'=>'该翻译已经存在']);
            }
			try {
                TranslatorService::add($data);
			} catch (\Exception $e) {
				$this->error($e->getMessage());
			}
			return json(['status'=>'00','msg'=>'添加成功']);
		}
	}

	/*修改*/
	function update(){
		if (!$this->request->isPost()){
			$id = $this->request->get('id','','intval');
			if(!$id) $this->error('参数错误');
			$this->view->assign('info',checkData(TranslatorDb::getInfo($id)));
			return $this->display('record/translator/info');
		}else{
            $postField = 'id,name,birth_day,sex,address,passport_idcard_no';
			$data = $this->request->only(explode(',',$postField),'post');
			try {
                TranslatorService::update($data);
			} catch (\Exception $e) {
				$this->error($e->getMessage());
			}
			return json(['status'=>'00','msg'=>'修改成功']);
		}
	}


	/*删除*/
	function delete(){
		$idx =  $this->request->post('ids', '', 'strip_tags');
		if(!$idx) $this->error('参数错误');
		try{
			$where['id'] = explode(',',$idx);
            TranslatorService::delete($where);
		}catch(\Exception $e){
			$this->error($e->getMessage());
		}
		return json(['status'=>'00','msg'=>'操作成功']);
	}

	/*查看数据*/
	function view(){
		$id = $this->request->get('id','','intval');
		if(!$id) $this->error('参数错误');
		try{
			$this->view->assign('info',checkData(TranslatorDb::getInfo($id)));
			return $this->display('record/customer/view');
		} catch (\Exception $e){
			$this->error($e->getMessage());
		}
	}


    function getThumbPic(){
        $detail = $this->request->post('detail','','strval');
        preg_match_all('/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/',$detail,$allImg);
        return json(['status'=>'00','imgurl'=>$allImg[1][0]]);
    }

    public function checkName(){
        $name=$this->request->param('name', '', 'strip_tags,trim');
        $ret = TranslatorDb::countList(['name'=>$name]);
        if($ret) {
            return json(['status'=>'01','msg'=>'该客户已经存在']);
        }
        return json(['status'=>'00','msg'=>'']);
    }



}

